//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceLabel = CommunityServer.ControlPanel.Controls.ResourceLabel;
namespace CommunityServer.ControlPanel.Controls
{
	/// <summary>
	///		Summary description for GroupEditControl.
	/// </summary>
	public class GroupEditControl : UserControl
	{

		#region Public Properties

		private int groupID = -1;
		public int GroupID
		{
			get{return groupID;}
			set{groupID = value;}
		}
		private ApplicationType applicationType;
		public ApplicationType ApplicationType
		{
			get{return applicationType;}
			set{applicationType = value;}
		}

		public string CurrentGroupName
		{
			get{return GroupName.Text;}
		}

		#endregion

		#region Child Controls

		protected ResourceLabel Resourcelabel2;
		protected RequiredFieldValidator ForumGroupNameValidator;
		protected TextBox GroupName;
		protected FormLabel Formlabel1;
		protected TextBox GroupDesc;
		protected FormLabel tt;

		#endregion

		private bool IsEdit = false;
		private bool IsNew
		{
			get{return GroupID == -1;}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				BindData();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		protected void BindData() 
		{
			
			Group group = CurrentSectionGroup();
			if( group != null ) 
			{
				LoadGroup( group );
			}
		}

		protected Group CurrentSectionGroup()
		{
			CSContext cntx = CSContext.Current;
			switch(this.ApplicationType)
			{
				case ApplicationType.Gallery:
					if(!cntx.User.IsGalleryAdministrator)
						throw new CSException( CSExceptionType.AdministrationAccessDenied );
						
					return GalleryGroups.GetGroup(this.GroupID, false, true, true);

				case ApplicationType.Weblog:
					if(!cntx.User.IsBlogAdministrator)
						throw new CSException( CSExceptionType.AdministrationAccessDenied );

					return WeblogGroups.GetWeblogGroup(this.groupID, false, true, true);

				case ApplicationType.FileGallery:
					if(!cntx.User.IsFileAdministrator)
						throw new CSException( CSExceptionType.AdministrationAccessDenied );

					return CommunityServer.Files.Components.Folders.GetFolderGroup(this.GroupID, false, true);
				
				default:
					return null;
					//throw new ArgumentOutOfRangeException(string.Format("The application type {0} is not supported!", this.ApplicationType.ToString() ) );
			}
				
		}

		protected virtual void LoadGroup( Group group ) 
		{
			GroupName.Text = group.Name;
			GroupDesc.Text = group.Description;
		}
		


		public bool Save()
		{
			if(Page.IsValid)
			{
				Group group = CurrentSectionGroup();

				if(group == null)
				{
					group = new Group(GroupName.Text.Trim());
					group.ApplicationType = this.ApplicationType;
				}

				return SaveGroup( group );

			}
			return false;
		}

		protected virtual bool SaveGroup( Group group ) 
		{
			group.Name = GroupName.Text.Trim();
			group.Description = GroupDesc.Text.Trim();

			foreach(Group g in GetGroups())
			{
				if((g.Name.ToLower()  == group.Name.ToLower() ) && (g.GroupID != group.GroupID))
				{
					ForumGroupNameValidator.IsValid = false;
					return false;
				}
			}

			if(Page.IsValid)
			{
				if(IsNew)
					Groups.AddGroup( group ); 
				else
					Groups.UpdateGroup( group );
				IsEdit = true;
				GetGroups();
				return true;
			}
			else
				return false;
		}

		protected ArrayList GetGroups()
		{
			switch(this.ApplicationType)
			{
				case ApplicationType.Gallery:
					return GalleryGroups.GetGroups(false, true, IsEdit);

				case ApplicationType.Weblog:
					return WeblogGroups.GetWeblogGroups(false, true, IsEdit);

				case ApplicationType.FileGallery:
					return CommunityServer.Files.Components.Folders.GetFolderGroups(false, true, IsEdit);

				default:
					throw new ArgumentOutOfRangeException(string.Format("The application type {0} is not supported!", this.ApplicationType.ToString() ) );
			}
		}
	}
}
